package com.ego.passport.service.Impl;

import com.ego.commons.constant.Constant;
import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.CartPojo;
import com.ego.commons.pojo.EgoResult;
import com.ego.commons.pojo.ShopCart;
import com.ego.commons.utils.CookieUtils;
import com.ego.commons.utils.ServletUtil;
import com.ego.dubbo.service.TbUserDubboService;
import com.ego.passport.service.TbUserService;
import com.ego.pojo.TbUser;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Service
public class TbUserServiceImpl implements TbUserService {
    @DubboReference
    private TbUserDubboService tbUserDubboService;


    @Override
    public EgoResult checkUser(String param, int type) {
        TbUser tbUser = new TbUser();
        if (type==1){
            tbUser.setUsername(param);
        }else if (type==3){
            tbUser.setEmail(param);
        }else if (type==2){
            tbUser.setPhone(param);
        }
        TbUser tbUser1 = tbUserDubboService.selectByUser(tbUser);
        //如果查询的TbUser对象为空，说明数据库中没有相关数据，可以注册
        return tbUser1==null?EgoResult.ok():EgoResult.error("");
    }

    @Override
    public EgoResult register(TbUser user) {
        Date date = new Date();
        user.setCreated(date);
        user.setUpdated(date);
        //使用Md5加密密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));

        try {
            int i=tbUserDubboService.insertTbUser(user);
            if (i==1){
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("注册用户失败");
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public TbUser login(TbUser tbUser) {
        tbUser.setPassword(DigestUtils.md5DigestAsHex(tbUser.getPassword().getBytes()));
        TbUser loginUser = tbUserDubboService.login(tbUser);
        //合并购物车 ：将临时购物车和用户购物车数据合并
        if (loginUser!=null){
            //获取临时购物车
            ShopCart tempShopCart = ShopCart.getShopCartByCookie();

            if(tempShopCart==null){
                tempShopCart = new ShopCart();
            }
            List<CartPojo> tempCartList = tempShopCart.getCartList();
            //获取用户购物车
            ShopCart userShopCart = (ShopCart) redisTemplate.opsForValue().get(Constant.LOGIN_USER_REDIS_SHOP_CART_KEY_PREFIX+loginUser.getUsername());
            if(userShopCart==null){
                userShopCart = new ShopCart();
            }
            //如果临时购物车没有任何数据没有必要同步,有数据才同步
            if(tempCartList.size()>0){
                for (CartPojo cartPojo : tempCartList) {
                    //将临时购物车的数据添加到用户购物车
                    userShopCart.addToCart(cartPojo);
                }
            }
            //存储用户购物车
            redisTemplate.opsForValue().set(Constant.LOGIN_USER_REDIS_SHOP_CART_KEY_PREFIX+loginUser.getUsername(),userShopCart);

            //清空临时购物车
            CookieUtils.deleteCookie(ServletUtil.getRequest(),ServletUtil.getResponse(),Constant.CART_NAME);
        }
        return  loginUser;
    }
}
